<script setup lang="ts">
import { type FormInstance, Message } from '@arco-design/web-vue'
import { DictApi } from '@/apis'
import { useForm } from '@/hooks'
import * as Regexp from '@/utils/regexp'

const emit = defineEmits<{
  (e: 'change'): void
}>()

const visible = ref(false)
const loading = ref(false)
const readonly = ref(true)

const dataId = ref('')
const dataDetail = ref<DictApi.DictDetailResp>()

const isCreateMode = computed(() => dataId.value === '')
const title = computed(() => (isCreateMode.value ? '创建字典' : '修改字典'))

const formRef = ref<FormInstance>()
const { form, resetForm } = useForm({
  name: null,
  code: null,
  reserved: false,
  description: null,
})

const rules: FormInstance['rules'] = {
  name: [{ required: true, match: Regexp.Name, message: '名称只能含有中文、数字、英文、下划线、短横线,长度为2-32位' }],
  code: [{ required: true, match: Regexp.Code, message: '编码只能含有英文、数字、下划线、短横线,以字母开头,长度为2-32位' }],
}

// 查询详情
const getDataDetail = async () => {
  dataDetail.value = await DictApi.getDictDetail(dataId.value)
  for (const key in form) {
    form[key] = dataDetail.value?.[key]
  }
  loading.value = false
}

// 打开编辑
const edit = async (id: string) => {
  dataId.value = id
  loading.value = true
  visible.value = true
  readonly.value = false
  getDataDetail()
}

// 打开新增
const create = async (dictId: string) => {
  dataId.value = ''
  loading.value = false
  visible.value = true
  readonly.value = false

  form.dictId = dictId
}

// 保存
const onSave = async () => {
  const isInvalid = await formRef.value?.validate()
  if (isInvalid) {
    return false
  }
  if (isCreateMode.value) {
    await DictApi.createDict(form)
    Message.success('添加成功')
  } else {
    await DictApi.updateDict(form, dataId.value)
    Message.success('修改成功')
  }
  emit('change')
  visible.value = false
  return true
}

// 取消
const onCancel = () => {
  readonly.value = true
  for (const key in form) {
    form[key] = dataDetail.value?.[key]
  }
  formRef.value?.clearValidate()
}

// 重置
const reset = () => {
  formRef.value?.resetFields()
  resetForm()
}

defineExpose({ open, edit, create })
</script>

<template>
  <ProFormPanel
    v-model:visible="visible"
    mode="modal"
    :title="title"
    :loading="loading"
    :footer="true"
    @confirm="onSave"
    @close="reset"
  >
    <a-form ref="formRef" layout="vertical" :model="form" :rules size="medium">
      <a-card title="基础信息" :bordered="false" class="section">
        <a-form-item label="字典名称" field="name">
          <a-input v-model.trim="form.name" placeholder="请输入字典名称" :max-length="32" />
        </a-form-item>
        <a-form-item label="字典编码" field="code">
          <a-input v-model.trim="form.code" placeholder="请输入字典编码" :max-length="32" :disabled="!isCreateMode" />
        </a-form-item>
        <a-form-item label="是否预置" field="reserved">
          <DictSelect v-model="form.reserved" code="BooleanEnum" type="boolean" />
        </a-form-item>
        <a-form-item label="描述" field="description" :span="24">
          <a-textarea v-model.trim="form.description" placeholder="请输入描述" :max-length="255" show-word-limit />
        </a-form-item>
      </a-card>
    </a-form>
  </ProFormPanel>
</template>

<style lang="less" scoped></style>
